package com.hashimoto.core.util;

import com.hashimoto.core.remote.client.ClientType;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;

public final class NettyAttributeUtil {
	
	private static String USER_NAME = "userName";
	
	private static String CLIENT_ID = "clientId";
	
	private static String USER_TYPE = "userType";
	
	private static String IS_LOGIN = "is_login";
	
	public static String username(Channel channel) {
		return (String)channel.attr(AttributeKey.valueOf(USER_NAME)).get();
	}

	public static void username(Channel channel,String username) {
		channel.attr(AttributeKey.valueOf(USER_NAME)).set(username);
	}
	
	public static Integer clientId(Channel channel) {
		return (Integer)channel.attr(AttributeKey.valueOf(CLIENT_ID)).get();
	}
	
	public static void clientId(Channel channel,int clientId) {
		channel.attr(AttributeKey.valueOf(CLIENT_ID)).set(clientId);;
	}
	
	public static void userType(Channel channel,String clientType) {
		channel.attr(AttributeKey.valueOf(USER_TYPE)).set(ClientType.valueOf(clientType));
	}
	
	public static ClientType userType(Channel channel) {
		return (ClientType)channel.attr(AttributeKey.valueOf(USER_TYPE)).get();
	}
	
	public static boolean isLogin(Channel channel) {
		Object obj = channel.attr(AttributeKey.valueOf(IS_LOGIN)).get();
		if(obj == null) {
			return false;
		}
		return (boolean)obj;
	}
	
	public static void setLogin(Channel channel,boolean isLogin) {
		channel.attr(AttributeKey.valueOf(IS_LOGIN)).set(isLogin);
	}
	
	public static void set(Channel channel,String key,Object value) {
		channel.attr(AttributeKey.valueOf(key)).set(value);
	}
	
	public static Object get(Channel channel,String key) {
		return channel.attr(AttributeKey.valueOf(key)).get();
	}
}
